﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.Admin == "undefined") Axis.Admin = {};
if (typeof Axis.Admin.QuestionPath == "undefined") Axis.Admin.QuestionPath = {};
Axis.Admin.QuestionPath.List = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        thisObj.EventClick();

        var cmaskdate = new CMaskDate({
            dateTimePicker: true

        });
        cmaskdate.Init($("#txtExpriedDate"));
        thisObj.Change();
    },
    Change: function () {
        var thisObj = this;
        $("#ctl00_AdministratorBody_ddlProjectList").bind("change", function () {
            // $('#imgUserAdd').click();
            thisObj.Grid.Reload();
            Axis.Admin.QuestionPath.Entry.Reset();
        }).trigger("change");
    },
    Grid: {
        ID: "#GridContents",
        PagesID: "#GridPages",
        GridObj: null,
        GridURL: 'Service/QuestionPathService.ashx?Action=GET',
        Init: function () {
            var thisObj = this;
            var options = {
                url: thisObj.GridURL,
                datatype: "json",
                colNames: [
                    'ID', 'Tên điều kiện', 'Ghi chú', 'Ưu tiên', 'Dự án', '', 'Status', '', 'Thao tác', '', ''
                ],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, hidden: true, frozen: true },
                    { name: 'Name', index: 'ID', width: 120, frozen: true, align: 'center' },
                    { name: 'Description', index: 'Description', width: 400, frozen: true },
                      { name: 'ZOrder', index: 'ClassName', hidden: true, width: 50, align: 'center' },
                    { name: 'NextQuestionID', index: 'ClassName', hidden: true, width: 1, align: 'center' },
                    { name: 'QuestionID', index: 'Class', hidden: true },
                    { name: 'StatusString', index: 'ScaleFrom', hidden: true, width: 90, align: 'center' },
                    { name: 'Status', index: 'ScaleFrom', width: 1, align: 'center', hidden: true },
                    { name: 'Action', index: 'ScaleFrom', width: 90, align: 'center' },
                    { name: 'ScaleFrom', index: 'ScaleFrom', width: 1, align: 'center', hidden: true },
                    { name: 'NextQuestionnaireMethod', index: 'EducationName', width: 1, align: 'center', hidden: true }
                ],
                onCellSelect: function (rowid, iCol, cellcontent, e) {
                },
                onSelectRow: function (rowid, status, e) {
                },
                ondblClickRow: function (rowid, iRow, iCol, e) {
                },
                afterInsertRow: function (rowid, aData, addData) {
                    var xA = '<img class="imgAction" src="Css/img/edit.png" id="PlaceOrder' + rowid + '" onclick="return ShowEdit(' + rowid + ')"/>';
                    $(thisObj.ID).setCell(rowid, 'Action', xA, {});

                },
                loadComplete: function () {
                    // ShowEdit();
                },

                height: 'auto',
                rownumbers: true,
                autowidth: true,
                //forceFit: true,
                shrinkToFit: false,
                rowNum: 10,
                pager: thisObj.PagesID,
                editurl: "Service/EditUser.ashx"
            };
            thisObj.GridObj = new CGrid(options);
            thisObj.GridObj.Init(thisObj.ID);
            thisObj.GridObj.NavGrid();
            thisObj.GridObj.FrozenColumns();
        },
        GetGridData: function () {
            return $(Axis.Admin.Project.Question.List.Grid.ID).jqGrid('getRowData');
        },
        Reload: function () {
            var thisObj = this;
            var newURL = thisObj.GridURL + "&ProjectID=" + $("#ctl00_AdministratorBody_ddlProjectList").val();
            $(thisObj.ID).clearGridData();
            $(thisObj.ID).jqGrid('setGridParam', { url: newURL }).trigger("reloadGrid");
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.GridObj) {
                return thisObj.GridObj.SelectRow();
            }
            return null;
        }
    },
    DialogFilter: {
        ID: "#UserFilter",
        Dialog: null,
        Init: function (type, opts) {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Filter",
                    width: 330,
                    height: 400,
                    buttons: [
                        {
                            text: "Search",
                            click: function (e) {
                                var data = {};
                                $("#UserFilter").find("input,select").each(function () {
                                    var $this = $(this);
                                    if ($this.attr("data-field")) {
                                        var key = $this.attr("data-field");
                                        var value = $this.val();
                                        if ($.isArray(value)) {
                                            data[key] = value.join(", ");
                                        }
                                        else {
                                            data[key] = value;
                                        }
                                    }
                                });
                                data = decodeURIComponent($.param(data));
                                $("#UsersGrid").jqGrid('setGridParam', { url: "Service/MemberService.ashx?Action=GET" + data, page: 1 }).trigger("reloadGrid");
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Cancel",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

                    ]
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        }
    },
    DialogEdit: {
        ID: "#FormEdit",
        Dialog: null,
        Init: function (action, opts) {
            var _width = $(window).width() - 10;
            var thisObj = this;
            //   Axis.Admin.Project.Question.List.ClearForm();

            if (thisObj.Dialog) {
                thisObj.Dialog.Open();

            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Edit User",
                    width: _width,
                    open: function () {
                        Axis.Admin.QuestionPath.Entry.Reset(opts);
                    }
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
            var buttonText = "";
            switch (action) {
                case "INSERT":
                    thisObj.Dialog.SetTitle("Logic câu hỏi");
                    $(thisObj.ID).find("[data-role='INSERT']").show();
                    $(thisObj.ID).find("#txtLoginName").focus();
                    buttonText = "Thêm";
                    break;
                case "UPDATE":
                    thisObj.Dialog.SetTitle("Hiệu chỉnh logic: " + "");
                    $.each(opts, function (key, value) {
                        $(thisObj.ID).find("[data-field=" + key + "]").val(value);
                    });
                    Axis.Admin.QuestionPath.Entry.LoadCondition(opts);
                    Axis.Admin.QuestionPath.Entry.LoadConditionDetail();
                    buttonText = "Hiệu chỉnh";
                    break;
            }
            var buttons = [
                        {
                            text: buttonText,
                            click: function (e) {
                                Axis.Admin.QuestionPath.Entry.Update(action, opts);
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Xóa trắng",
                            click: function (e) {
                                Axis.Admin.QuestionPath.Entry.Reset();
                            }
                        },
                        {
                            text: "Thoát",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

            ];
            $(thisObj.ID).dialog('option', 'buttons', buttons);
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }

        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        }
    },
    DialogFilter: {
        Init: function () { }
    },
    EventClick: function () {
        var thisObj = this;
        $("#imgUserAdd").click(function () {
            if ($("#ctl00_AdministratorBody_ddlProjectList").val() != "") {
                thisObj.DialogEdit.Init("INSERT");
                $("#txtID").val(0);
                //  $("#ddlProjectList").val(1);
            }
            else {
                alert("Vui lòng chọn dự án");
            }

        });
        $("#imgUserEdit").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID)
                thisObj.DialogEdit.Init("UPDATE", rowData);
            else alert("Please Select Row");
        });

        $("#imgUserDel").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID) {
                $.CAlert.Confirm({
                    Title: "Xóa câu hỏi",
                    Content: $.string.Format("Bạn có chắc muốn nhánh câu hỏi: [{0}].", rowData.Name)
                }, function (isOK) {
                    if (isOK)
                        Axis.Admin.QuestionPath.Entry.Update("DELETE", rowData);
                });
            }
            else {
                $.CAlert.Alert({
                    Title: "Xóa nhánh câu hỏi",
                    Content: "Vui lòng chọn nhánh câu hỏi cần xóa."
                });
            }
        });
        //        $("#imgUserFilter").click(function () {
        //            thisObj.DialogFilter.Init();
        //        });
    }
};
$(function () {
    var thisObj = Axis.Admin.QuestionPath.List;
    thisObj.Init();

});
function ShowEdit(rowID) {
    var thisObj = Axis.Admin.QuestionPath.List;
    var rowData = $(thisObj.Grid.ID).jqGrid('getRowData', rowID);
    if (rowData && rowData.ID) {
        thisObj.DialogEdit.Init("UPDATE", rowData);
        // Axis.Admin.QuestionPath.Entry.LoadPathDetail(rowData);
    }

}